package com.example.smartagrisys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.smartagrisys.common.Result;
import com.example.smartagrisys.dto.FertilizingRecordDTO;
import com.example.smartagrisys.service.FertilizingRecordService;
import com.example.smartagrisys.vo.FertilizingRecordVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 施肥记录控制器
 */
@Slf4j
@RestController
@RequestMapping("/fertilizing-record")
@RequiredArgsConstructor
public class FertilizingRecordController {

    private final FertilizingRecordService fertilizingRecordService;

    /**
     * 获取当前用户的施肥记录
     * @return 施肥记录列表
     */
    @GetMapping
    public Result<List<FertilizingRecordVO>> getMyFertilizingRecords(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        String role = (String) request.getAttribute("role");

        if (!"farmer".equals(role)) {
            return Result.error("只有农户才能查看施肥记录");
        }

        List<FertilizingRecordVO> records = fertilizingRecordService.getFarmerAllFertilizingRecords(userId);
        return Result.success(records);
    }

    /**
     * 分页获取当前用户的施肥记录
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<Page<FertilizingRecordVO>> getMyFertilizingRecordsPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        String role = (String) request.getAttribute("role");

        if (!"farmer".equals(role)) {
            return Result.error("只有农户才能查看施肥记录");
        }

        Page<FertilizingRecordVO> page = fertilizingRecordService.getFarmerFertilizingRecords(userId, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 添加施肥记录
     * @param dto 施肥记录数据
     * @return 添加结果
     */
    @PostMapping
    public Result<Long> addFertilizingRecord(@RequestBody FertilizingRecordDTO dto, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        String role = (String) request.getAttribute("role");

        if (!"farmer".equals(role)) {
            return Result.error("只有农户才能添加施肥记录");
        }

        Long id = fertilizingRecordService.add(dto, userId);
        return Result.success(id);
    }

    /**
     * 更新施肥记录
     * @param id 记录ID
     * @param dto 施肥记录数据
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public Result<Boolean> updateFertilizingRecord(
            @PathVariable Long id,
            @RequestBody FertilizingRecordDTO dto,
            HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        String role = (String) request.getAttribute("role");

        if (!"farmer".equals(role)) {
            return Result.error("只有农户才能更新施肥记录");
        }

        dto.setId(id);
        boolean result = fertilizingRecordService.update(dto, userId);
        return Result.success(result);
    }

    /**
     * 删除施肥记录
     * @param id 记录ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteFertilizingRecord(@PathVariable Long id, HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        String role = (String) request.getAttribute("role");

        if (!"farmer".equals(role)) {
            return Result.error("只有农户才能删除施肥记录");
        }

        boolean result = fertilizingRecordService.delete(id, userId);
        return Result.success(result);
    }

    /**
     * 获取施肥记录详情
     * @param id 记录ID
     * @return 施肥记录详情
     */
    @GetMapping("/{id}")
    public Result<FertilizingRecordVO> getFertilizingRecordDetail(@PathVariable Long id, HttpServletRequest request) {
        String role = (String) request.getAttribute("role");

        if (!"farmer".equals(role)) {
            return Result.error("只有农户才能查看施肥记录详情");
        }

        FertilizingRecordVO vo = fertilizingRecordService.getDetail(id);
        return Result.success(vo);
    }
}
